/*
*
* Copyright (c) 2011 Vaulting Systems International
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy 
* of this software and associated documentation files (the "Software"), to deal 
* in the Software without restriction, including without limitation the rights 
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies 
* of the Software, and to permit persons to whom the Software is furnished to do  
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all  
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE  
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
package com.ekeyman.securesave.service;

import java.math.BigInteger;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.ws.client.core.WebServiceTemplate;

import com.ekeyman.securesave.service.EkeymanService;
import com.ekeyman.securesavelib.shared.schema.CreateEncryptionKeysRequest;
import com.ekeyman.securesavelib.shared.schema.CreateEncryptionKeysResponse;
import com.ekeyman.securesavelib.shared.schema.DeleteEncryptionKeysRequest;
import com.ekeyman.securesavelib.shared.schema.DeleteEncryptionKeysResponse;
import com.ekeyman.securesavelib.shared.schema.GetEncryptionKeysRequest;
import com.ekeyman.securesavelib.shared.schema.GetEncryptionKeysResponse;
import com.ekeyman.securesavelib.shared.schema.ObjectFactory;
import com.ekeyman.securesavelib.shared.schema.RegisterAppDeviceRequest;
import com.ekeyman.securesavelib.shared.schema.RegisterAppDeviceResponse;
import com.ekeyman.securesave.utils.DigitalSignature;
import com.ekeyman.securesavelib.dto.AppDeviceApiKeys;
import com.ekeyman.securesavelib.dto.EncryptionKeys;

public class EkeymanServiceImpl implements EkeymanService{

    private static final Log logger = LogFactory.getLog(EkeymanServiceImpl.class);

    private static final ObjectFactory jaxbObjectFactory = new ObjectFactory();
    
    private WebServiceTemplate webServiceTemplate;
    
    public EkeymanServiceImpl(WebServiceTemplate webServiceTemplate){
    	this.webServiceTemplate = webServiceTemplate;
    }
    
	@Override
	public EncryptionKeys createEncryptionKeys(String resourceUri, 
			String publicKey, String privateKey,
			int keyBytesLength, int ivBytesLength, int saltBytesLength, 
			int iterationCountMax, String location) {

		logger.debug("createEncryptionKeys...");
		
		CreateEncryptionKeysRequest request = jaxbObjectFactory.createCreateEncryptionKeysRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setKeyBytesLength(BigInteger.valueOf(keyBytesLength)); 
		request.setIvBytesLength(BigInteger.valueOf(ivBytesLength));
		request.setSaltBytesLength(BigInteger.valueOf(saltBytesLength));
		request.setIterationCountMax(BigInteger.valueOf(iterationCountMax));
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, Integer.toString(keyBytesLength), 
				Integer.toString(ivBytesLength), Integer.toString(saltBytesLength), 
				Integer.toString(iterationCountMax), location);
		
		request.setDigitalSignature(computedDigitalSignature);
		
		CreateEncryptionKeysResponse createEncryptionKeysResponse =
			(CreateEncryptionKeysResponse) webServiceTemplate.marshalSendAndReceive(request);
		
		EncryptionKeys encryptionKeys = new EncryptionKeys();
		encryptionKeys.setKeyBytesLength(keyBytesLength); 
		encryptionKeys.setKeyBytes(createEncryptionKeysResponse.getKeyBytes());
		encryptionKeys.setIvBytesLength(ivBytesLength);
		encryptionKeys.setIvBytes(createEncryptionKeysResponse.getIvBytes());
		encryptionKeys.setSaltBytesLength(saltBytesLength);
		encryptionKeys.setSaltBytes(createEncryptionKeysResponse.getSaltBytes());		
		encryptionKeys.setIterationCount(createEncryptionKeysResponse.getIterationCount().intValue());
		return encryptionKeys;
	}

	@Override
	public boolean deleteEncryptionKeys(String resourceUri, String publicKey,
			String privateKey, String location) {
		
		logger.debug("deleteEncryptionKeys...");
		
		DeleteEncryptionKeysRequest request = jaxbObjectFactory.createDeleteEncryptionKeysRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, location);

		request.setDigitalSignature(computedDigitalSignature);
		
		DeleteEncryptionKeysResponse deleteEncryptionKeysResponse =
			(DeleteEncryptionKeysResponse) webServiceTemplate.marshalSendAndReceive(request);
		
		return deleteEncryptionKeysResponse.isOperationSuccessful();
		
	}

	@Override
	public EncryptionKeys getEncryptionKeys(String resourceUri,
			 String publicKey, String privateKey, String location) {

		logger.debug("getEncryptionKeys...");
		
		GetEncryptionKeysRequest request = jaxbObjectFactory.createGetEncryptionKeysRequest();
		
		request.setResourceUri(resourceUri);
		request.setPublicKey(publicKey);
		request.setLocation(location);
		
		//create computed digital signature from public/private key and data
		String computedDigitalSignature = DigitalSignature.createDigitalSignature(
				resourceUri, publicKey, privateKey, location);

		request.setDigitalSignature(computedDigitalSignature);
		
		GetEncryptionKeysResponse getEncryptionKeysResponse =
			(GetEncryptionKeysResponse) webServiceTemplate.marshalSendAndReceive(request);
		
		EncryptionKeys encryptionKeys = new EncryptionKeys();
		encryptionKeys.setKeyBytesLength((getEncryptionKeysResponse.getKeyBytesLength().intValue())); 
		encryptionKeys.setKeyBytes(getEncryptionKeysResponse.getKeyBytes());
		encryptionKeys.setIvBytesLength(getEncryptionKeysResponse.getIvBytesLength().intValue());
		encryptionKeys.setIvBytes(getEncryptionKeysResponse.getIvBytes());
		encryptionKeys.setSaltBytesLength(getEncryptionKeysResponse.getSaltBytesLength().intValue());
		encryptionKeys.setSaltBytes(getEncryptionKeysResponse.getSaltBytes());	
		encryptionKeys.setIterationCount(getEncryptionKeysResponse.getIterationCount().intValue());
		
		return encryptionKeys;
	}

	@Override
	public AppDeviceApiKeys registerAppDevice(String apiKey,
			String application, String device) {
		
		logger.debug("registerAppDevice...");
		
		RegisterAppDeviceRequest request = jaxbObjectFactory.createRegisterAppDeviceRequest();
		
		request.setApiKey(apiKey);
		request.setApplication(application);
		request.setDevice(device);
		
		RegisterAppDeviceResponse registerAppDeviceResponse =
			(RegisterAppDeviceResponse) webServiceTemplate.marshalSendAndReceive(request);
		
		AppDeviceApiKeys appDeviceApiKeys = new AppDeviceApiKeys();
		appDeviceApiKeys.setPublicKey(registerAppDeviceResponse.getPublicKey());
		appDeviceApiKeys.setPrivateKey(registerAppDeviceResponse.getPrivateKey()); 
		
		return appDeviceApiKeys;	

	}
}
